<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="my" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- <el-tree :data="list" :props="defaultProps" @node-click="handleNodeClick"> -->
        <el-tree :data="list" :props="defaultProps">
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="news" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑子部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="hDel(data.id)">删除子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog
        :title="isEdit?'编辑部门':'新增部门'"
        :visible.sync="showDianlog"
        width="30%"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @closed="hClose"
      >
        <!-- <addOrEdit v-if="showDianlog" :id="curId" :is-edit="isEdit" @success="hSuccess" /> -->
        <!-- <addOrEdit :id="curId" ref="deptRef" :is-edit="isEdit" @success="hSuccess" /> -->
        <addOrEdit :id="curId" ref="deptRef" :is-edit="isEdit" :origin-list="originList" @close="showDianlog=false" @success="hSuccess" />
        <!-- <span slot="footer" class="dialog-footer">
          <el-button @click="showDianlog = false">取 消</el-button>
          <el-button type="primary" @click="showDianlog = false">确 定</el-button>
        </span> -->
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartments } from '@/api/departments'
import { transDataToTreeData } from '@/utils/index'
import addOrEdit from './deptDialog.vue'
export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      list: [],
      showDianlog: false,
      curId: '',
      isEdit: false,
      originList: [],
      defaultProps: {
        children: 'children',
        label: 'manager'
      }
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    hClose() {
      this.$refs.deptRef.resetForm()
    },
    hSuccess() {
      this.loadDepartments()
      this.showDianlog = false
    },
    // handleNodeClick(data) {
    //   console.log(data)
    // },
    hAdd(id) {
      this.curId = id
      this.isEdit = false
      this.showDianlog = true
    },
    hEdit(id) {
      this.curId = id
      this.isEdit = true
      this.$nextTick(() => {
        this.$refs.deptRef.loadDepartmentDetail()
      })
      this.showDianlog = true
    },
    hDel(id) {
      this.$confirm('确定要删除该部门吗', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delDepartments(id)
        if (res.code === 10000) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.loadDepartments()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    async loadDepartments() {
      try {
        const res = await getDepartments()
        console.log('所有的部门信息', res)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.originList = res.data.depts.map(item => {
          const { id, code, name, pid } = item
          return { id, code, name, pid }
        })
        this.$message.success(res.message)
        res.data.depts.shift()
        this.list = transDataToTreeData(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
